{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-2-a157494e4726>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-2-a157494e4726>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    from socket\u001b[0m\n\u001b[1;37m                ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "## socket 的服务端 server\n",
    "\n",
    "from socket \n",
    "\n",
    "server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
    "server.bind((0.0.0.0, 8000))\n",
    "server.listen()\n",
    "sock, addr = server.accept()\n",
    "\n",
    "## 获取从客户端发送的数据\n",
    "## 一次获取1K数据\n",
    "data = sock.recv(1024)\n",
    "print(data.decode(\"utf-8\"))\n",
    "sock.send(\"hello {}\".format(data.decode(\"utf-8\")).encode(\"utf-8\"))\n",
    "server.close()\n",
    "sock.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## socket 的客户端 client\n",
    "\n",
    "from socket\n",
    "\n",
    "client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
    "client.connect(('127.0.0.1', 8000))\n",
    "client.send(\"bobby\".encode(\"utf-8\"))\n",
    "data = client.recv(1024)\n",
    "print(data.decode(\"utf-8\"))\n",
    "client.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
